﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;
using System.Data;

/// <summary>
/// Summary description for ModeloEliminarBodega
/// </summary>
public class ModeloEliminarBodega
{
    private static String idBodegaObjetivo;
    private static String idBodegaReemplazo;
    private Conexion conn;
    
    public ModeloEliminarBodega()
	{
        idBodegaObjetivo = "";
        idBodegaReemplazo = "";
        conn = new Conexion();
	}

    public void setModel(String i1 , String i2)
    {
        idBodegaObjetivo = i1;
        idBodegaReemplazo = i2;
    }

    public DataTable getOtrasBodegas(String codBodega, String deptoBodega)
    {
        try
        {

            DataTable dt = new DataTable();

            DataColumn c1 = new DataColumn();
            c1.ColumnName = "Nombre";
            DataColumn c3 = new DataColumn();
            c3.ColumnName = "depto";

            dt.Columns.Add(c1);
            dt.Columns.Add(c3);


            String query = "SELECT BODEGA.NOMBREBODEGA , BODEGA.IDBODEGA " +
                           "FROM BODEGA INNER JOIN TIPO_BODEGA ON (BODEGA.IDTIPOBODEGA = TIPO_BODEGA.IDTIPOBODEGA) " +
                           "WHERE BODEGA.IDBODEGA !="+codBodega+" AND TIPO_BODEGA.NOMBRETIPOBODEGA='"+deptoBodega+"'";

            bool connected = conn.iniciarConexion();

            if (connected == true)
            {
                SqlDataReader rows = null;
                SqlCommand comm = new SqlCommand(query, conn.getConn());
                rows = comm.ExecuteReader();
                while (rows.Read())
                {
                    DataRow r = dt.NewRow();
                    r["Nombre"] = rows[0].ToString();
                    r["depto"] = rows[1].ToString();
                    dt.Rows.Add(r);

                }

                conn.terminarConexion();

                return dt;
            }

            else
            {

                conn.terminarConexion();
                return null;
            }
        }

        catch (Exception exc)
        {

            return null;
        }

    }

    public int eliminarBodega() //retornos: 1:tudo bem , -1:tudo mal, -3: o no me pude conectar o excepcion
    {
        try
        {
            bool conectar = conn.iniciarConexion();
            if (conectar)
            {

                SqlCommand cmd = new SqlCommand("eliminarBodega", conn.getConn());
                cmd.CommandType = CommandType.StoredProcedure;

                cmd.Parameters.Add(new SqlParameter("@bodegaObjetivo",
                                                           SqlDbType.Int,
                                                           0,
                                                           "bodegaObjetivo"
                                                        )
                                        );

                cmd.Parameters.Add(new SqlParameter("@bodegaReemplazo",
                                                           SqlDbType.Int,
                                                           0,
                                                           "bodegaReemplazo"
                                                        )
                                        );


                cmd.Parameters.Add(new SqlParameter("@outParam",
                                                           SqlDbType.Int,
                                                           0,
                                                           ParameterDirection.Output,
                                                           false,
                                                           0,
                                                           0,
                                                           "outParam",
                                                           DataRowVersion.Default,
                                                           null
                                                        )
                                        );

                cmd.Parameters[0].Value = idBodegaObjetivo;
                cmd.Parameters[1].Value = idBodegaReemplazo;
                cmd.UpdatedRowSource = UpdateRowSource.OutputParameters;

                int per = cmd.ExecuteNonQuery();

                int result = (int)cmd.Parameters["@outParam"].Value;

                conn.terminarConexion();

                return result;

            }

            else
            {
                conn.terminarConexion();
                return -3;
            }
        }

        catch (Exception exc)
        {
            return -3;
        }
    
    }
}